<template>
  <div class="accounts">
    <!-- 新增按钮 -->
    <el-row>
  <el-col :span="24"><div class="grid-content bg-purple-dark">
        <el-button type="primary" size="mini" @click="add()" >新建账户</el-button>
      
    </div>
    <!-- 条查 -->
    <div class="tiaocha">
    <el-form :inline="true" :model="form" class="demo-form-inline">
  <el-form-item label="关键词" >
    <el-input v-model="form.accountName" placeholder="账户名称" size="mini"></el-input>
  </el-form-item>
   <el-form-item label="账户状态">
    <el-select v-model="form.status" placeholder="请选择" size="mini">
      <el-option label="启用" value="启用"></el-option>
      <el-option label="停用" value="停用"></el-option>
    </el-select>
  </el-form-item>
   <el-form-item label="机构类型">
              <el-select v-model="form.id" placeholder="请选择" size="mini">
                <el-option
                  v-for="item in optionsList" 
                  :key="item.id"
                  :label="item.institutionType"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="search()" size="mini">查询</el-button>
    <el-button type="primary" @click="Reset()" size="mini">重置</el-button>
  </el-form-item>
</el-form>
 </div>
    </el-col>
</el-row>
    <!-- 表格 -->
      
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="账户编码" width="auto">
      </el-table-column>
      <el-table-column prop="accountName" label="账户名称" width="auto">
      </el-table-column>
      <el-table-column prop="institutionType" label="机构类型" width="auto">
        
      </el-table-column>
      <el-table-column prop="institutionName" label="机构名称" width="auto">
      </el-table-column>
      <el-table-column prop="account" label="账号" >
      </el-table-column>
      <el-table-column prop="openingBalance" label="初期余额" width="auto">
      </el-table-column>
      <el-table-column prop="currentBalance" label="当前余额" width="auto">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="auto">
      </el-table-column>
      <el-table-column prop="remark" label="备注" width="auto">
      </el-table-column>
                                  <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                    <el-button
            type="info"
            disabled
            size="mini"
            v-show="scope.row.defalutStatus == 1"
            >默认账户</el-button
          >
          <el-button
            type="primary"
            size="mini"
            v-show="scope.row.defalutStatus == 2"
            @click="updateDefault(scope.row.id)"
            >设为默认</el-button
          >
                  <el-button
                    size="mini"
                    type="danger"
                    @click="updateStatus(scope.row.id)"
                    v-show="scope.row.status == '启用'"
                    >停用</el-button
                  >
                  <el-button
                    size="mini"
                    type="success"
                    @click="updateStatusTow(scope.row.id)"
                    v-show="scope.row.status == '停用'"
                    >启用</el-button
                  >
             
                   <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
                        >修改</el-button
                    >
                 
                </template>
              </el-table-column>
    </el-table>
             <!-- 分页 -->
             <el-row>
                    <el-pagination
                      @size-change="handleSizeChange"
                      @current-change="handleCurrentChange"
                      :current-page="pageNum"
                      :page-sizes="[2, 5, 10, 20]"
                      :page-size="pageSize"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="total"
                    >
                    </el-pagination>
                    </el-row>
        
    </el-row>
     <!-- 新增弹框 -->
         <el-dialog :title = title :visible.sync="dialogVisible" width="40%">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="账户名称">
          <el-input
            v-model="formInline.accountName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
                 <el-form-item label="机构类型">
          <el-input
            v-model="formInline.institutionType"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
         <el-form-item label="期初余额">
          <el-input
            v-model="formInline.openingBalance"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
          <el-form-item label="机构名称">
          <el-input
            v-model="formInline.institutionName"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
          <el-form-item label="账号">
          <el-input
            v-model="formInline.account"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="formInline.remark"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addlist()" v-if="num == 1">新增</el-button>
           <el-button type="primary" @click="updateP()" v-if="num == 2">修改</el-button>
                  
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios"
export default {
  name: "accounts",
  data() {
    return {
      //设为默认按钮
      dddd: {
        defalutStatus: 1,
      },
      // 提交按钮
      num: "0",
      title: "",
      //新增
      formInline: {
        accountName: "",
        institutionType: "",
        openingBalance: "",
        institutionName: "",
        account: "",
        remark: "",
      },
      //弹框
      dialogVisible: false,
      //停用
      ttt: {
        status: "",
      },
      //动态下拉
      optionsList: [],
      tableData: [],
      //分页
      pageNum: 1,
      pageSize: 5,
      total: 0,
      form: {
        id: "",
        accountName: "",
        status: "",
        institutionType: "",
      },
    };
  },
  created() {
    this.search();
    this.searchholidayList();
  },
  methods: {
    //改为默认
    updateDefault(val) {
      axios.post("Accounts/updateDefault?id=" + val).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("已改为默认!");
          this.search();
        } else {
          this.$message.error("修改失败!");
        }
      });
    },
    //修改
    updateP() {
      this.$http
        .post("Accounts/updateAccounts", this.formInline)
        .then((res) => {
          this.formInline = res.data;
          if (res.data.code == 200) {
            this.$message({
              message: "恭喜你，" + res.data.msg,
              type: "success",
            });
            this.dialogVisible = false;
            this.search();
          } else {
            this.$message.error("错了哦，" + res.data.msg);
          }
        });
    },
    //新增
    addlist() {
      this.$http.post("Accounts/addAccounts", this.formInline).then((res) => {
        this.formInline = res.data;
        if (res.data.code == 200) {
          this.$message({
            message: "恭喜你，" + res.data.msg,
            type: "success",
          });
          this.dialogVisible = false;
          this.search();
        } else {
          this.$message.error("错了哦，" + res.data.msg);
        }
      });
    },
    //新增弹框
    add() {
      this.formInline="";
      (this.dialogVisible = true), (this.title = "新增");
      this.num = 1;
    },
    //修改为启用
    updateStatusTow(val) {
      this.$http
        .post("Accounts/updateStatusqi?id=" + val, this.ttt)
        .then((res) => {
          if (res.data.result == true) {
            this.$message.success("已启用!");
            this.search();
          } else {
            this.$message.error("修改成功!");
          }
        });
    },
    //修改为停用
    updateStatus(val) {
      this.$http
        .post("Accounts/updateStatusting?id=" + val, this.ttt)
        .then((res) => {
          if (res.data.result == true) {
            this.$message.success("已停用!");
            this.search();
          } else {
            this.$message.error("修改成功!");
          }
        });
    },
    //回显
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.title = "修改";
      this.$http.get("Accounts/findAccountsById?id=" + row.id).then((res) => {
        this.formInline = res.data;
        this.num = 2;
      });
    },
    // //动态下拉列表框
    searchholidayList() {
      this.$http
        .post("Accounts/findOptions")
        .then((res) => {
          this.optionsList = res.data;
        })
        .catch((error) => {
          console.error("获取部门列表失败:", error);
        });
    },
    //重置按钮
    Reset() {
      (this.form.accountName = ""),
        (this.form.status = ""),
        (this.form.id = ""),
        this.search();
    },
    //条查 分页
    search() {
      this.$http
        .post(
          "Accounts/findAccountsPagination?pageNum=" +
            this.pageNum +
            "&pageSize=" +
            this.pageSize,
          this.form
        )
        .then((res) => {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        });
    },
    //分页
    handleCurrentChange(val) {
      this.pageNum = val;
      this.search();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.search();
    },
  },
};
</script>

<style scoped>
</style>